package com.example.alipay_10_3.controller;

import com.example.alipay_10_3.Vo.AlipayVo;
import com.example.alipay_10_3.Vo.Price;
import com.example.alipay_10_3.server.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@CrossOrigin
public class PayController {

    @Autowired
    private PayService payService;
    /**
     * 下单支付
     */
    @RequestMapping("/alipay")
    private Object pay(@RequestBody Price price) throws Exception {
        Date date = new Date();
        //可以方便地修改日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HHmmss");
        //转换格式为yyyyMMdd
        String Out_trade_no = dateFormat.format(date);
        //根据时间设置唯一订单号，精确到秒
        AlipayVo aliPayVo = new AlipayVo();
        aliPayVo.setOut_trade_no(Out_trade_no);
        aliPayVo.setId(price.getId());
        aliPayVo.setSubject(price.getName());
        aliPayVo.setTotal_amount(price.getPrice());

        return payService.Alipay(aliPayVo);
    }
}
